package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
//权限管理服务

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    //注入Dao
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);

    }

    //权限管理分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //当前页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //每页记录数
        Integer pageSize = queryPageBean.getPageSize();
        //过滤条件
        String queryString = queryPageBean.getQueryString();
        //完成分页查询,基于Mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);
        if(queryString!=null){
            queryString='%'+queryString+'%';
        }
        Page<Permission> page = permissionDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Permission> rows = page.getResult();

        return new PageResult(total,rows);
    }

    @Override
    public void deleteById(Integer id) {
        long count=permissionDao.findCountByPermissionId(id);
        if (count>0){
           throw  new RuntimeException();
        }
        permissionDao.deleteById(id);
    }

    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    @Override
    public List<Integer> findpermissionIdsByroleId(Integer id) {
        List<Integer> roles = permissionDao.findpermissionIdsByroleId(id);
        return roles;
    }
}
